﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using Telerik.Web.UI;
using SalePartial.BLL;
using SalePartial.Model;
using System.Web.Security;

public partial class AddNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //修改页头
            Page.Header.Title = String.Format("环球零件资源网--网站管理向导");

             // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                //string radalertscript = "<script language='javascript'>function f(){radalert('Welcome to RadWindow for <strong>ASP.NET AJAX</strong>!', 330, 210); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";

                string radalertscript = "<script language='javascript'>function f(){radopen(\"../../../Login/NoPermission.aspx\", null); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);

                //Response.Redirect(GlobalSetting.UrlHomePage, true);
            }

            if (HttpContext.Current.Session["NewsID"] != null)
            {
                this.hidID.Value = HttpContext.Current.Session["NewsID"].ToString();

                XPNews news = new XPNews();
                XPNewsInfo newsInfo = news.GetXPNews(new Guid(this.hidID.Value));
                if (newsInfo != null)
                {
                    this.txtTitle.Text = newsInfo.Title;
                    this.dplCatagory.SelectedValue = newsInfo.CaID.ToString();
                    this.RadEditor1.Content = newsInfo.Content;
                }
            }
            if (HttpContext.Current.Session["NewsReadOnly"] != null)
            {
                this.hideSts.Value = HttpContext.Current.Session["NewsReadOnly"].ToString();

                if (Boolean.Parse(this.hideSts.Value) == false)
                {
                    btnAdd.Visible = false;
                    RadEditor1.Enabled = false;
                    txtTitle.Enabled = false;
                    dplCatagory.Enabled = false;
                }
                else
                {
                    btnAdd.Visible = true;
                    RadEditor1.Enabled = true;
                    txtTitle.Enabled = true;
                    dplCatagory.Enabled = true;
                }
            }
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
            XPNewsInfo news = new XPNewsInfo();
            news.Title = this.txtTitle.Text.Trim();
            news.CaID = new Guid(this.dplCatagory.SelectedValue);
            news.Content = this.RadEditor1.Content;
            news.CreateUserID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
            string nid = this.hidID.Value;
            Guid id = Guid.Empty;
            int addok = 0;
            if (nid != null && nid != string.Empty)
            {
                id = new Guid(nid);
                news.NewsID = id;

                addok = new XPNews().Update(news);
            }
            else
            {
                addok = new XPNews().Insert(news);
            }
            if (addok == 0)
            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ok", "<script>alert('新闻操作成功！');location.href='NewsManage.aspx'</script>");
                //if (id > 0)
                //{
                //    // Response.Write("<script>location.href='newsmanager.aspx'</script>");
                //}
                this.txtTitle.Text = "";
                this.RadEditor1.Content = "";
                this.hidID.Value = "";
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "err", "<script>alert('新闻操作失败！');</script>");
                // this.Page.RegisterStartupScript("err", "<script>alert('新闻操作失败！');</script>");
            }
        }
   

}
